﻿using System;
using System.Collections;
using System.Text;
using TextEditor.TextDocument.Document;
namespace TextEditor
{
     /***
      * 内容操作类 
      */
    public class ZYContent
    {
        private bool bolAutoClearSelection = true;
        private bool bolLineEndFlag = false;
        private bool bolModified = false;
        private int intLastXPos = -1;
        private int intSelectLength = 0;  //选中元素长度
        public int intSelectStart = 0;    //当前选中元素下标
        private IZYContentDocument myDocument = null;
        private ArrayList myElements = null; //元素集合
        private string strFixLenText = null;
        public bool isDragMove = false; //是否可以拖动
        private bool CheckSelectStart()
        {
            if (this.myElements == null)
            {
                return false;
            }
            return ((this.intSelectStart >= 0) && (this.intSelectStart <= (this.myElements.Count - 1)));
        }

        public int DeleteCurrentElement()
        {
            if (((this.myElements != null) && (this.myElements.Count != 0)) && this.CheckSelectStart())
            {
                ZYTextElement deleteElement = (ZYTextElement)this.myElements[this.intSelectStart];

                if (deleteElement.Parent.CanDelete || deleteElement.CanDelete || (deleteElement is ZYTextContainer ? ((ZYTextContainer)deleteElement).minLength > 0 : false))
                {
                    return 0;
                }

                if (deleteElement != this.myElements[this.myElements.Count - 1])
                {
                    int num = this.myDocument.isDeleteElement(deleteElement);
                    if ((num == 0) && deleteElement.Parent.RemoveChild(deleteElement))
                    {
                        this.bolModified = true;
                        this.myDocument.ContentChanged();
                        this.FixSelection();
                    }
                    return num;
                }
            }
            return 1;
        }

        public int DeletePreElement()
        {
             if ((this.myElements != null) && (this.myElements.Count != 0) && (this.intSelectStart > 0) && (this.intSelectStart < this.myElements.Count))
            {
                ZYTextElement deleteElement = (ZYTextElement)this.myElements[this.intSelectStart - 1];
                ZYTextElement deleteNextElement = (ZYTextElement)this.myElements[this.intSelectStart];
                if (deleteElement.Parent.CanDelete || deleteElement.CanDelete || (deleteElement.Parent is ZYTextDiv ? deleteElement.Parent != deleteNextElement.Parent : false))
                {
                    return 0;
                }
                if (!(deleteElement is ZYTextContainer))
                {
                    bool flag = false;
                    if (deleteElement.Parent.WholeElement)
                    {
                        flag = true;
                    }
                    if ((deleteElement.Parent is ZYTextBlock) && (deleteElement == deleteElement.Parent.GetLastElement()))
                    {
                        flag = true;
                    }
                    if (flag)
                    {
                        if(deleteElement.Parent.minLength>0 || deleteElement.Parent.Parent.CanDelete)
                        {
                            return 0;
                        }
                        deleteElement = deleteElement.Parent;
                    }
                }
                int index = this.myElements.IndexOf(deleteElement);
                int num2 = this.myDocument.isDeleteElement(deleteElement);
                if (num2 == 0)
                {
                    if (deleteElement.Parent.RemoveChild(deleteElement))
                    {
                        this.bolModified = true;
                        //this.myDocument.ContentChanged();
                        this.SetSelection(index, 0);
                    }
                    return num2;
                }
                if ((num2 == 2))
                {
                    this.bolModified = true;
                    //this.myDocument.ContentChanged();
                    this.SetSelection(index, 0);
                }
                return num2;
            }
            return 1;
        }

        public void DeleteSeleciton()
        {
            if ((this.myElements != null) && (this.myElements.Count != 0))
            {
                int absSelectStart = this.AbsSelectStart;
                int absSelectEnd = this.AbsSelectEnd;
                int num3 = (this.intSelectLength > 0) ? this.intSelectLength : -this.intSelectLength;
                bool flag = false;
                ArrayList selectElements = this.GetSelectElements();
                ArrayList myList = new ArrayList();
                ZYTextContainer parent = null;
                foreach (ZYTextElement element in selectElements)
                {
                    if (!CanDeleted(element))
                    {
                        this.myDocument.ContentChanged();
                        return;
                    }
                    if ((parent != null) && (parent != element.Parent))
                    {
                        parent.RemoveChildRange(myList);
                        myList.Clear();
                        parent.RefreshLine();
                    }
                    parent = element.Parent;
                    flag = true;
                    if (this.myDocument.isDeleteElement(element) == 0)
                    {
                        myList.Add(element);
                    }
                }
                if (myList.Count > 0)
                {
                    parent.RemoveChildRange(myList);
                    parent.RefreshLine();
                    flag = true;
                }
                if (flag)
                {
                    this.bolModified = true;
                    this.FixSelection();
                    if (this.myDocument != null)
                    {
                        this.myDocument.ContentChanged();
                    }
                    this.SetSelection(absSelectStart, 0);
                    this.FixSelection();
                }
            }
        }

        public bool CanDeleted(ZYTextElement element)
        {
            if (element is ZYTextContainer)
            {
                ZYTextContainer container = (ZYTextContainer)element;
                if (container.CanDelete || container.minLength > 0)
                {
                    return false;
                }
            }
            ZYTextDocument document = (ZYTextDocument)this.myDocument;
            if (element.Parent != null)
            {
                if (element.Parent is ZYTextContainer)
                {
                    if (((ZYTextContainer)element.Parent).CanDelete)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public bool FindText(string strText)
        {
            if ((strText != null) && (strText.Length != 0))
            {
                this.GetFixLenText();
                if (this.strFixLenText != null)
                {
                    int index = this.strFixLenText.IndexOf(strText, this.SelectStart);
                    if (index >= 0)
                    {
                        this.SetSelection(index + strText.Length, -strText.Length);
                    }
                    return (index >= 0);
                }
            }
            return false;
        }

        public bool FindText(string strText,int selectStart)
        {
            if ((strText != null) && (strText.Length != 0))
            {
                this.GetFixLenText();
                if (this.strFixLenText != null)
                {
                    int index = this.strFixLenText.IndexOf(strText, selectStart);
                    if (index >= 0)
                    {
                        this.SetSelection(index + strText.Length, -strText.Length);
                    }
                    return (index >= 0);
                }
            }
            return false;
        }

        private int FixIndex(int index)
        {
            if (index <= 0)
            {
                return 0;
            }
            if (index >= this.myElements.Count)
            {
                return (this.myElements.Count - 1);
            }
            return index;
        }

        private void FixSelection()
        {
            if (this.intSelectStart >= this.myElements.Count)
            {
                this.intSelectStart = this.myElements.Count - 1;
            }
            if (this.intSelectStart < 0)
            {
                this.intSelectStart = 0;
            }
            if ((this.intSelectLength > 0) && ((this.intSelectStart + this.intSelectLength) > this.myElements.Count))
            {
                this.intSelectLength = 0;
            }
            if ((this.intSelectLength < 0) && ((this.intSelectStart + this.intSelectLength) < 0))
            {
                this.intSelectLength = 0;
            }
        }

        public ArrayList GetCurrentLineElements()
        {
            this.intSelectStart = this.FixIndex(this.intSelectStart);
            ZYTextElement element = (ZYTextElement) this.myElements[this.intSelectStart];
            int lineIndex = element.LineIndex;
            int num2 = 0;
            for (int i = this.intSelectStart - 1; i >= 0; i--)
            {
                element = (ZYTextElement) this.myElements[i];
                if (element.LineIndex != lineIndex)
                {
                    num2 = i + 1;
                    break;
                }
            }
            ArrayList list = new ArrayList();
            for (int j = num2; j < this.myElements.Count; j++)
            {
                element = (ZYTextElement) this.myElements[j];
                if (element.LineIndex != lineIndex)
                {
                    return list;
                }
                list.Add(element);
            }
            return list;
        }

        public string GetCurrentLineHeadBlank()
        {
            this.intSelectStart = this.FixIndex(this.intSelectStart);
            ArrayList currentLineElements = this.GetCurrentLineElements();
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < currentLineElements.Count; i++)
            {
                ZYTextChar ch = currentLineElements[i] as ZYTextChar;
                if ((ch == null) || !char.IsWhiteSpace(ch.Char))
                {
                    break;
                }
                builder.Append(ch.Char);
            }
            return builder.ToString();
        }

        public ZYTextElement GetElementAt(int x, int y)
        {
            if (((this.myDocument != null) && (this.myDocument.Lines != null)) && (this.myDocument.Lines.Count > 0))
            {
                ArrayList list = new ArrayList();
                foreach (ZYTextLine line in this.myDocument.Lines)
                {
                    if (y >= line.RealTop && y <= (line.RealTop + line.Height))
                    {
                        list.Add(line);
                    }
                }
                int num2 = x;
                foreach (ZYTextLine line2 in list)
                {
                    foreach (ZYTextElement element in line2.Elements)
                    {
                        if ((x > element.RealLeft) && (x <= ((element.RealLeft + element.Width) + element.WidthFix)))
                        {
                            return element;
                        }
                    }
                }
            }
            return null;
        }

        public ArrayList GetElementsRange(int Index1, int Index2)
        {
            if (this.myElements == null)
            {
                return null;
            }
            ArrayList list = new ArrayList();
            int num = 0;
            if (Index1 > Index2)
            {
                num = Index1;
                Index1 = Index2;
                Index2 = num;
            }
            Index1 = this.FixIndex(Index1);
            Index2 = this.FixIndex(Index2);
            for (int i = Index1; i < Index2; i++)
            {
                list.Add(this.myElements[i]);
            }
            return list;
        }

        internal string GetFixLenText()
        {
            if (this.myElements == null)
            {
                return null;
            }
            if (this.bolModified || (this.strFixLenText == null))
            {
                char[] chArray = new char[this.myElements.Count];
                ZYTextChar ch = null;
                for (int i = 0; i < this.myElements.Count; i++)
                {
                    ch = this.myElements[i] as ZYTextChar;
                    if (ch == null)
                    {
                        chArray[i] = '\x0001';
                    }
                    else
                    {
                        chArray[i] = ch.Char;
                    }
                }
                this.strFixLenText = new string(chArray);
            }
            return this.strFixLenText;
        }

        public ZYTextElement GetNextElement(ZYTextElement refElement)
        {
            int index = this.myElements.IndexOf(refElement);
            if ((index >= 0) && (index < (this.myElements.Count - 1)))
            {
                return (ZYTextElement) this.myElements[index + 1];
            }
            return null;
        }

        public ZYTextChar GetPreChar()
        {
            ArrayList selectElements = this.GetSelectElements();
            ZYTextElement currentElememnt = this.CurrentElement;
            if (selectElements.Count > 0)
            {
                ZYTextElement element = (ZYTextElement)selectElements[0];
                if (element is ZYTextChar)
                {
                    if (element.Index == currentElememnt.Index)
                    {
                        return (ZYTextChar)element;
                    }
                    else
                    {
                        return (selectElements[selectElements.Count - 1] is ZYTextChar) ? (ZYTextChar)selectElements[selectElements.Count - 1] : null;
                    }
                }
            }
            else
            {
                if (currentElememnt != null && currentElememnt.Index > 0)
                {
                    if (this.Elements[currentElememnt.Index - 1] is ZYTextChar)
                    {
                        return (ZYTextChar)this.Elements[currentElememnt.Index - 1];
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 返回当前字符前面两个的字符
        /// </summary>
        /// <returns></returns>
        public ZYTextChar GetTwoPreChar()
        {
            for (int i = ((this.intSelectStart == 0) && (this.myElements.Count > 1)) ? 1 : (this.intSelectStart - 2); i >= 0; i--)
            {
                if (this.myElements[i] is ZYTextChar)
                {
                    return (ZYTextChar)this.myElements[i];
                }
            }
            return null;
        }

        public ZYTextElement GetPreElement(ZYTextElement refElement)
        {
            int index = this.myElements.IndexOf(refElement);
            if (index >= 1)
            {
                return (ZYTextElement) this.myElements[index - 1];
            }
            return null;
        }

        public string GetPreWord()
        {
            int preWordIndex = this.GetPreWordIndex();
            StringBuilder builder = new StringBuilder();
            ZYTextChar ch = null;
            if (preWordIndex >= 0)
            {
                for (int i = preWordIndex; i < this.intSelectStart; i++)
                {
                    ch = this.myElements[i] as ZYTextChar;
                    if ((ch == null) || !char.IsLetter(ch.Char))
                    {
                        break;
                    }
                    builder.Append(ch.Char);
                }
            }
            if (builder.Length == 0)
            {
                return null;
            }
            return builder.ToString();
        }

        public string GetPreWord(ZYTextElement myElement)
        {
            int preWordIndex = this.GetPreWordIndex(myElement);
            StringBuilder builder = new StringBuilder();
            ZYTextChar ch = null;
            if (preWordIndex >= 0)
            {
                int x = this.myElements.IndexOf(myElement);
                for (int i = preWordIndex; i <= x; i++)
                {
                    ch = this.myElements[i] as ZYTextChar;
                    if ((ch == null) || !char.IsLetter(ch.Char))
                    {
                        break;
                    }
                    builder.Append(ch.Char);
                }
            }
            if (builder.Length == 0)
            {
                return null;
            }
            return builder.ToString();
        }

        public int GetPreWordIndex()
        {
            int num = -1;
            ZYTextLine currentLine = this.CurrentLine;
            for (int i = this.intSelectStart - 1; i >= 0; i--)
            {
                if (!(this.myElements[i] is ZYTextChar))
                {
                    return num;
                }
                ZYTextChar ch = (ZYTextChar) this.myElements[i];
                if (!char.IsLetter(ch.Char) || (ch.OwnerLine != currentLine))
                {
                    return num;
                }
                num = i;
            }
            return num;
        }

        public int GetPreWordIndex(ZYTextElement myElement)
        {
            int num = -1;
            if ((myElement == null) || !this.myElements.Contains(myElement))
            {
                return -1;
            }
            for (int i = this.myElements.IndexOf(myElement) - 1; i >= 0; i--)
            {
                if (!(this.myElements[i] is ZYTextChar) || !char.IsLetter((this.myElements[i] as ZYTextChar).Char))
                {
                    return num;
                }
                num = i;
            }
            return num;
        }

        public string GetRangeText(int intStartIndex, int intEndIndex)
        {
            intStartIndex = this.FixIndex(intStartIndex);
            intEndIndex = this.FixIndex(intEndIndex);
            StringBuilder builder = new StringBuilder();
            for (int i = intStartIndex; i <= intEndIndex; i++)
            {
                builder.Append(((ZYTextElement) this.myElements[i]).ToZYString());
            }
            return builder.ToString();
        }

        public string GetSelectedText()
        {
            ArrayList selectElements = this.GetSelectElements();
            if ((selectElements != null) && (selectElements.Count > 0))
            {
                return ZYTextElement.GetElementsText(selectElements);
            }
            return null;
        }

        public ZYTextElement GetSelectElement()
        {
            if (this.intSelectLength == 1)
            {
                return (ZYTextElement) this.myElements[this.intSelectStart];
            }
            if (this.intSelectLength == -1)
            {
                return (ZYTextElement) this.myElements[this.intSelectStart - 1];
            }
            return null;
        }

        public ArrayList GetSelectElements()
        {
            if (this.myElements == null)
            {
                return null;
            }
            ArrayList list = new ArrayList();
            int absSelectEnd = this.AbsSelectEnd;
            for (int i = this.AbsSelectStart; i <= absSelectEnd; i++)
            {
                list.Add(this.myElements[i]);
            }
            return list;
        }

        public ArrayList GetSelectTableElements()
        {
            if (this.myElements == null)
            {
                return null;
            }
            ArrayList list = new ArrayList();
            int absSelectEnd = this.AbsSelectEnd;
            for (int i = this.AbsSelectStart; i <= absSelectEnd + 1; i++)
            {
                list.Add(this.myElements[i]);
            }
            return list;
        }

        public ArrayList GetSelectParagraph()
        {
            if (this.myElements == null)
            {
                return null;
            }
            ArrayList list = new ArrayList();
            int absSelectEnd = this.AbsSelectEnd;
            for (int i = this.AbsSelectStart; i < this.myElements.Count; i++)
            {
                if (this.myElements[i] is ZYTextParagraph)
                {
                    list.Add(this.myElements[i]);
                    if (i > absSelectEnd)
                    {
                        return list;
                    }
                }
            }
            return list;
        }

        public string GetSelectText()
        {
            return ZYTextElement.GetElementsText(this.GetSelectElements());
        }

        public string GetText()
        {
            return ZYTextElement.GetElementsText(this.myElements);
        }

        public bool HasSelected()
        {
            return (this.intSelectLength != 0);
        }

        public bool HasSelectedChar()
        {
            ArrayList selectElements = this.GetSelectElements();
            if ((selectElements != null) && (selectElements.Count > 0))
            {
                foreach (object obj2 in selectElements)
                {
                    if (obj2 is ZYTextChar)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public bool HasSelectedText()
        {
            ArrayList selectElements = this.GetSelectElements();
            if ((selectElements != null) && (selectElements.Count > 0))
            {
                foreach (ZYTextElement element in selectElements)
                {
                    if (element.isTextElement())
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public ZYTextChar InsertChar(char vChar)
        {
            if ((this.myElements != null) && (this.myElements.Count != 0))
            {
                if (this.intSelectStart < 0)
                {
                    this.intSelectStart = 0;
                }
                if (this.intSelectStart >= this.myElements.Count)
                {
                    this.intSelectStart = this.myElements.Count - 1;
                }
                ZYTextChar newElement = null;
                ZYTextChar preChar = this.GetPreChar();
                //###
                if (preChar != null)
                {
                    if (preChar.Sub || preChar.Sup)
                        preChar = this.GetTwoPreChar();
                }
               
                ZYTextElement currentElement = this.CurrentElement;
                ZYTextContainer parent = currentElement.Parent;

                if (currentElement.OwnerDocument.ContentChangeLog != null)
                {
                    currentElement.OwnerDocument.ContentChangeLog.CanLog = false;
                }
                newElement = currentElement.OwnerDocument.CreateChar(vChar);
                //if (preChar != null)
                //{
                //    preChar.Attributes.CopyTo(newElement.Attributes);
                //    newElement.UpdateAttrubute();
                //}
                if (currentElement.OwnerDocument.ContentChangeLog != null)
                {
                    currentElement.OwnerDocument.ContentChangeLog.CanLog = true;
                }
                if (parent.InsertBefore(newElement, currentElement))
                {
                    this.bolModified = true;
                    return newElement;
                }
            }
            return null;
        }
        /// <summary>
        /// 是否刷新
        /// </summary>
      

        //public void InsertElement(ZYTextElement NewElement)
        //{
        //    if ((this.myElements != null) && (this.myElements.Count != 0))
        //    {
        //        ZYTextElement refElement = (ZYTextElement) this.myElements[this.intSelectStart];

        //        refElement.Bounds = new System.Drawing.Rectangle(0, 0, 11, 18);
        //        if (refElement.Parent.InsertBefore(NewElement, refElement))
        //        {
        //            this.bolModified = true;
        //            if (this.myDocument != null)
        //            {
        //                this.myDocument.ContentChanged();
        //            }
        //            this.AutoClearSelection = true;
        //            this.MoveSelectStart((int) (this.intSelectStart + 1));
        //        }
        //    }
        //}

        public void InsertElement(ZYTextBapfMark NewElement)
        {
            if ((this.myElements != null) && (this.myElements.Count != 0))
            {
                ZYTextElement myElement = (ZYTextElement)this.myElements[this.intSelectStart];
                if (myElement.Parent.InsertBeforeBapf(NewElement, myElement))
                {
                    this.AutoClearSelection = true;
                    if (this.myDocument != null)
                    {
                        this.MoveSelectStart((int)(this.intSelectStart) + 1);
                    }
                }
            }
        }


        public void InsertElement(ZYTextElement NewElement)
        {
             if ((this.myElements != null) && (this.myElements.Count != 0))
            {
                ZYTextElement myElement = (ZYTextElement)this.myElements[this.intSelectStart];
                if (myElement.Parent.InsertBefore(NewElement, myElement))
                {
                    this.bolModified = true;
                    if (this.myDocument != null && !(NewElement is ZYTextParagraph))
                    {
                        this.myDocument.ContentChanged();
                    }
                    this.AutoClearSelection = true;
                    if (this.myDocument != null && !(NewElement is ZYTextParagraph))
                    {
                        this.MoveSelectStart((int)(this.intSelectStart) + 1);
                    }
                }
            }
        }

        public void InsertElement(ZYTextElement NewElement,ZYTextElement odlElement,int type)
        {
            if ((this.myElements != null) && (this.myElements.Count != 0))
            {
                ZYTextElement myElement = null;
                myElement = odlElement;
                if (type == 0)
                {
                    if (myElement.Parent.InsertNextDiv(NewElement, myElement))
                    {
                        this.bolModified = true;
                        if (this.myDocument != null && !(NewElement is ZYTextParagraph))
                        {
                            this.myDocument.ContentChanged();
                        }
                        this.AutoClearSelection = true;
                        if (this.myDocument != null && !(NewElement is ZYTextParagraph))
                        {
                            this.MoveSelectStart((int)(this.intSelectStart) + 1);
                        }
                    }
                }
                else
                {

                    if (myElement.Parent.InsertBefore(NewElement, myElement))
                    {
                        this.bolModified = true;
                        if (this.myDocument != null && !(NewElement is ZYTextParagraph))
                        {
                            this.myDocument.ContentChanged();
                        }
                        this.AutoClearSelection = true;
                        if (this.myDocument != null && !(NewElement is ZYTextParagraph))
                        {
                            this.MoveSelectStart((int)(this.intSelectStart) + 1);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 插入元素
        /// </summary>
        /// <param name="parent">父元素</param>
        /// <param name="NewElement">要插入的元素</param>
        /// <param name="refElement"></param>
        /// <param name="pos">0.插入当前选中元素前面 1.插入当前选中元素后面</param>
        public void InsertElement(ZYTextContainer parent, ZYTextElement NewElement, ZYTextElement refElement, int pos)
        {
            if ((this.myElements != null) && (this.myElements.Count != 0))
            {
                if (pos == 0)
                {
                    if (parent.InsertBefore(NewElement, refElement))
                    {
                        this.bolModified = true;
                        if (this.myDocument != null)
                        {
                            this.myDocument.ContentChanged();
                        }
                        this.AutoClearSelection = true;
                        this.MoveSelectStart((int) (this.intSelectStart + 1));
                    }
                }
                else if ((pos == 1) && parent.InsertAfter(NewElement, refElement))
                {
                    this.bolModified = true;
                    if (this.myDocument != null)
                    {
                        this.myDocument.ContentChanged();
                    }
                    this.AutoClearSelection = true;
                    this.MoveSelectStart((int) (this.intSelectStart + 1));
                }
            }
        }

        /***
         * 插入元素集合
         */
        public void InsertRangeElements(ArrayList myList)
        {
            if (((this.myElements != null) && (this.myElements.Count != 0)) && ((myList != null) && (myList.Count != 0)))
            {
                int count = this.Elements.Count;
                ZYTextElement refElement = (ZYTextElement) this.myElements[this.intSelectStart];
                ZYTextContainer parent = refElement.Parent;
                int maxlength = parent.maxLength;
                if (maxlength > 0 && maxlength < parent.IntLength + myList.Count)
                {
                    return;
                }
                parent.CanDelete = parent.CanDelete;
                parent.InsertRangeBefore(myList, refElement);
                parent.RefreshLine();
                this.bolModified = true;
                if (this.myDocument != null)
                {
                    this.myDocument.ContentChanged();
                }

                this.AutoClearSelection = true;
                this.MoveSelectStart((int)(this.intSelectStart + (this.Elements.Count - count)));
            }
        }

        /***
         * 插入字符串 支持多行文本
         */
        public void InsertString(string strText)
        {
            if ((this.myElements != null) && (this.myElements.Count != 0))
            {
                ZYTextChar ch = null;
                ZYTextChar ch2 = null;
                for (int i = this.intSelectStart - 1; i >= 0; i--)
                {
                    if (this.myElements[i] is ZYTextChar)
                    {
                        ch2 = (ZYTextChar) this.myElements[i];
                        break;
                    }
                }
                ZYTextElement refElement = (ZYTextElement) this.myElements[this.intSelectStart];
                ZYTextContainer parent = refElement.Parent;
                int maxlength = parent.maxLength;
                if (maxlength > 0 && maxlength < parent.IntLength + strText.Length)
                {
                    return;
                }
                ArrayList myList = new ArrayList();
                refElement.OwnerDocument.ContentChangeLog.CanLog = false;
                for (int j = 0; j < strText.Length; j++)
                {
                    if (strText[j] == '\n' || strText[j]== '\r')
                    {
                        myList.Add(new ZYTextParagraph());
                    }
                    else
                    {
                        ch = refElement.OwnerDocument.CreateChar(strText[j]);
                        if (ch2 != null)
                        {
                            //复制属性
                            //ch2.Attributes.CopyTo(ch.Attributes);
                        }
                        myList.Add(ch);
                    }
                }
                refElement.OwnerDocument.ContentChangeLog.CanLog = true;
                if (parent.CanDelete) { return; }
                parent.InsertRangeBefore(myList, refElement);
                this.bolModified = true;
                if (this.myDocument != null)
                {
                    this.myDocument.ContentChanged();
                }
                this.AutoClearSelection = true;
                this.MoveSelectStart((int) (this.intSelectStart + strText.Length));
            }
        }

        public bool isCurrentElement(ZYTextElement myElement)
        {
            return ((this.CurrentElement == myElement) && (this.intSelectLength == 0));
        }

        public bool isSelected(ZYTextElement myElement)
        {
            if ((this.intSelectLength == 0) || (myElement == null))
            {
                return false;
            }
            int index = myElement.Index;
            return ((((this.intSelectLength > 0) && (index >= this.intSelectStart)) && (index < (this.intSelectStart + this.intSelectLength))) || (((this.intSelectLength < 0) && (index >= (this.intSelectStart + this.intSelectLength))) && (index < this.intSelectStart)));
        }

        public void MoveDownOneLine()
        {
            ZYTextLine nextLine = this.NextLine;
            if (nextLine != null)
            {
                if (this.intLastXPos <= 0)
                {
                    ZYTextElement element = (ZYTextElement) this.myElements[this.intSelectStart];
                    this.intLastXPos = element.RealLeft;
                }
                foreach (ZYTextElement element2 in nextLine.Elements)
                {
                    if (element2.RealLeft >= this.intLastXPos)
                    {
                        this.MoveSelectStart(element2);
                        return;
                    }
                }
                this.MoveSelectStart(nextLine.LastElement);
            }
        }

        public void MoveEnd()
        {
            try
            {
                ZYTextLine currentLine = this.CurrentLine;
                if ((currentLine != null) && !this.bolLineEndFlag)
                {
                    this.intLastXPos = -1;
                    this.CurrentElement = currentLine.LastElement;
                    if (currentLine.LastElement.isNewLine())
                    {
                        this.MoveSelectStart(currentLine.LastElement);
                    }
                    else
                    {
                        this.MoveSelectStart((int) (this.myElements.IndexOf(currentLine.LastElement) + 1));
                        this.bolLineEndFlag = true;
                        ((ZYTextDocument) this.myDocument).UpdateTextCaret();
                    }
                }
            }
            catch
            {
            }
        }

        public void MoveHome()
        {
            ZYTextLine currentLine = null;
            currentLine = this.CurrentLine;
            if (currentLine != null)
            {
                this.intLastXPos = -1;
                int index = this.myElements.IndexOf(currentLine.FirstElement);
                int num2 = 0;
                foreach (ZYTextElement element in currentLine.Elements)
                {
                    ZYTextChar ch = element as ZYTextChar;
                    if ((ch == null) || !char.IsWhiteSpace(ch.Char))
                    {
                        num2 = currentLine.Elements.IndexOf(element);
                        break;
                    }
                }
                if ((num2 == 0) || (this.intSelectStart == (index + num2)))
                {
                    this.MoveSelectStart(index);
                }
                else
                {
                    this.MoveSelectStart((int) (index + num2));
                }
            }
        }

        public void MoveLeft()
        {
            this.intLastXPos = -1;
            if (this.intSelectStart > 0)
            {
                this.MoveSelectStart((int) (this.intSelectStart - 1));
            }
        }

        public void MoveRight()
        {
            this.intLastXPos = -1;
            if (this.intSelectStart < (this.myElements.Count - 1))
            {
                this.MoveSelectStart((int) (this.intSelectStart + 1));
            }
        }

        public bool MoveSelectStart(ZYTextElement refElement)
        {
            return ((this.myElements.IndexOf(refElement) >= 0) && this.MoveSelectStart(this.myElements.IndexOf(refElement)));
        }

        public bool MoveSelectStart(int index)
        {
            index = this.FixIndex(index);
            return this.SetSelection(index, this.bolAutoClearSelection ? 0 : ((this.intSelectLength + this.intSelectStart) - index));
        }

        public void MoveStep(int iStep)
        {
            ZYTextElement element = (ZYTextElement) this.myElements[this.intSelectStart];
            this.MoveTo(element.RealLeft, element.RealTop + iStep);
        }

        public void MoveTo(ZYTextElement element)
        {
            int index = this.myElements.IndexOf(element);
            this.MoveSelectStart(index);
            ((ZYTextDocument) this.myDocument).UpdateTextCaret();
        }

        public void MoveTo(int x, int y)
        {
            #region  旧方法
            if (this.myDocument != null)
            {
                ZYTextDocument myDocument = (ZYTextDocument)this.myDocument;
                ZYTextTable ownerTable = null;
                if (myDocument.InTable(myDocument.DocumentElement, x, y, ref ownerTable) && (ownerTable != null))
                {
                    this.MoveTo(ownerTable, x, y);
                }
                else
                {
                    this.intLastXPos = -1;
                    if (this.myDocument != null)
                    {
                        ZYTextLine line = null;
                        foreach (ZYTextLine line2 in this.myDocument.Lines)
                        {
                            if ((line2.RealTop + line2.Height) > y)
                            {
                                if (myDocument.GetInTable((ZYTextElement)line2.Elements[0]) != null && !(line2.Elements[0] is ZYTextTable))
                                {
                                    continue;
                                }
                                line = line2;
                                break;
                            }
                        }
                        if ((line == null) && (this.myDocument.Lines.Count > 0))
                        {
                            line = (ZYTextLine)this.myDocument.Lines[this.myDocument.Lines.Count - 1];
                        }
                        if (line != null)
                        {
                            bool flag = false;
                            int index = 0;
                            x -= line.RealLeft;
                            ZYTextElement element = null;
                            foreach (ZYTextElement element2 in line.Elements)
                            {
                                if (element2 is ZYTextTable && line.Elements.Count == 1)
                                {
                                    element = element2;
                                }
                                if ((x < (element2.Left + element2.Width)) && (x <= (element2.Left + (element2.Width / 2))))
                                {
                                    if (element2.Parent.WholeElement)
                                    {
                                        return;
                                    }
                                    element = element2;
                                    break;
                                }

                            }
                            if (line.Elements[0] is ZYTextTable && element != null)
                            {
                                if (element.RealTop > y)
                                {
                                    element = (ZYTextElement)line.Elements[0];
                                }                
                            }
                            if (element == null)
                            {
                                if (line.HasLineEnd)
                                {
                                    index = this.myElements.IndexOf(line.LastElement);
                                }
                                else
                                {
                                    index = this.myElements.IndexOf(line.LastElement) + 1;
                                    flag = true;
                                }
                            }
                            else
                            {
                                index = this.myElements.IndexOf(element);
                                if (index == -1)
                                {
                                    index = ReturnIndex(element);
                                }
                                flag = false;
                            }
                            if (index > this.myElements.Count)
                            {
                                index = this.myElements.Count - 1;
                                flag = false;
                            }
                            if (index < 0)
                            {
                                index = 0;
                                flag = false;
                            }
                            this.MoveSelectStart(index);
                            if (this.bolLineEndFlag != flag)
                            {
                                this.bolLineEndFlag = flag;
                                ((ZYTextDocument)this.myDocument).UpdateTextCaret();
                            }
                        }
                    }
                }
            }
            #endregion

            #region  新方法
            //ZYTextElement hoverElement = null;
            //foreach (ZYTextLine line in this.myDocument.Lines)
            //{             
            //    if(line.Container is ZYTextTable)
            //    {
            //        continue;
            //    }
            //    if (line.RealTop + line.FullHeight > y)
            //    {
            //        MoveELement(line, x, ref hoverElement;

            //    }
            //}
            #endregion
        }

        /// <summary>
        /// 判断这行里面有没有这个元素
        /// </summary>
        /// <param name="line"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        private bool MoveELement(ZYTextLine line,int x,ref ZYTextElement hoverElement)
        {
            foreach (ZYTextElement lineElement in line.Elements)
            {
                if ((x < (lineElement.Left + lineElement.Width)) && (x <= (lineElement.Left + (lineElement.Width / 2))))
                {
                    hoverElement = lineElement;
                }
            }
            return true;
        }

        public int ReturnIndex(ZYTextElement currentElement)
        {
            if (currentElement is ZYTextTable || currentElement is ZYTextTableCell || currentElement is ZYTextTableRow || currentElement is ZYTextDiv)
            {
                ZYTextContainer current = currentElement as ZYTextContainer;
                if (current.ChildCount > 0)
                {
                    ZYTextElement element = current.ChildElements[0] as ZYTextElement;
                    int index = this.myElements.IndexOf(element);
                    if (index == -1)
                    {
                        int havaIndex = ReturnIndex(element);
                        if (havaIndex != -1)
                        {
                            return havaIndex;
                        }
                    }
                    else
                    {
                        return index;
                    }
                }
            }
            return -1;
        }

        public void MoveTo(ZYTextTable table, int x, int y)
        {
            ZYTextElement lastElement = null;
            //int num = 0;
            foreach (ZYTextTableRow row in table.Rows)
            {
                foreach (ZYTextTableCell cell in row.Cells)
                {                                        
                    if (cell.Bounds.Contains(x, y))
                    {
                        foreach (ZYTextLine line in cell.Lines)
                        {
                            if ((line.RealTop - line.LineSpacing > y) || (y >= (line.RealTop + line.Height)))
                            {
                                continue;
                            }
                            x -=line.RealLeft;
                            foreach (ZYTextElement element2 in line.Elements)
                            {
                                if (!element2.OwnerDocument.Info.ShowAll)
                                {
                                    if (element2.Deleteted)
                                    {
                                        continue;
                                    }
                                }
                                if (element2 is ZYTextContainer)
                                {
                                   lastElement = GetIsCellElement((ZYTextContainer)element2, x, y);
                                   if (lastElement != null)
                                   {
                                       break;
                                   }
                                }
                                else
                                {
                                    if ((x < (element2.Left + element2.Width)) && (x <= (element2.Left + (element2.Width / 2))))
                                    {
                                        if (element2.Parent.WholeElement)
                                        {
                                            return;
                                        }
                                        lastElement = element2;
                                        break;
                                    }
                                }
                            }
                            if (lastElement == null)
                            {
                                lastElement = line.LastElement;
                                break;
                            }
                        }
                        if (lastElement == null)
                        {
                            lastElement = cell.GetLastElement();
                        }
                        break;
                    }
                    if (lastElement != null)
                    {
                        break;
                    }
                }
                if (lastElement != null)
                {
                    break;
                }
            }
            if (lastElement == null)
            {
                return;
            }
            int index = this.myElements.IndexOf(lastElement);
            bool flag = false;
            if (index > this.myElements.Count)
            {
                index = this.myElements.Count - 1;
                flag = false;
            }
            if (index < 0)
            {
                index = 0;
                flag = false;
            }
            this.MoveSelectStart(index);
            if (this.bolLineEndFlag != flag)
            {
                this.bolLineEndFlag = flag;
                ((ZYTextDocument) this.myDocument).UpdateTextCaret();
            }
        }

        public ZYTextElement GetIsCellElement(ZYTextContainer container, int x, int y)
        {
            ZYTextElement cellElement = null;
            if (container is ZYTextBlock)
            {               
                if (((container.Left + container.Width) > x))
                {
                    return container;
                }                
                foreach (ZYTextElement element3 in container.ChildElements)
                {              
                    if (!element3.OwnerDocument.Info.ShowAll)
                    {
                        if (element3.Deleteted)
                        {
                            continue;
                        }
                    }
                    if (element3 is ZYTextContainer)
                    {
                        cellElement = GetIsCellElement((ZYTextContainer)element3, x, y);
                        if (cellElement != null)
                        {
                            break;
                        }
                    }
                    else
                    {
                        if ((x < (element3.Left + element3.Width)) && (x <= (element3.Left + (element3.Width / 2))))
                        {
                            if (element3.Parent.WholeElement)
                            {
                                return null;
                            }
                            cellElement = element3;
                            break;
                        }
                    }
                }                
            }
            else
            {
                x -= container.intLeft;
                foreach (ZYTextLine line in container.Lines)
                {
                    if ((line.RealTop - line.LineSpacing> y) || (y >= (line.RealTop + line.Height)))
                    {
                        continue;
                    }
                    foreach (ZYTextElement element3 in line.Elements)
                    {                       
                        if (!element3.OwnerDocument.Info.ShowAll)
                        {
                            if (element3.Deleteted)
                            {
                                continue;
                            }
                        }

                        if (element3 is ZYTextContainer)
                        {
                            cellElement = GetIsCellElement((ZYTextContainer)element3, x, y);
                            if (cellElement != null)
                            {
                                break;
                            }
                        }
                        else
                        {
                            if ((x < (element3.Left + element3.Width)) && (x <= (element3.Left + (element3.Width / 2))))
                            {
                                if (element3.Parent.WholeElement)
                                {
                                    return null;
                                }
                                cellElement = element3;
                                break;
                            }
                        }
                    }
                    if (cellElement == null)
                    {
                        cellElement = line.LastElement;
                        break;
                    }
                }
            }
            return cellElement;
        }

        public void MoveToTable(int x, int y)
        {
            this.intLastXPos = -1;
            if (this.myDocument != null)
            {
                ArrayList list = new ArrayList();
                foreach (ZYTextLine line in this.myDocument.Lines)
                {
                    if ((line.RealTop + line.Height) > y)
                    {
                        list.Add(line);
                    }
                }
                if ((list.Count == 0) && (this.myDocument.Lines.Count > 0))
                {
                    list.Add((ZYTextLine) this.myDocument.Lines[this.myDocument.Lines.Count - 1]);
                }
                bool flag = false;
                int index = 0;
                ZYTextElement lastElement = null;
                int num2 = x;
                ZYTextLine line2 = null;
                foreach (ZYTextLine line3 in list)
                {
                    foreach (ZYTextElement element2 in line3.Elements)
                    {
                        ZYTextTableCell inTableCell = ((ZYTextDocument) this.myDocument).GetInTableCell(element2);
                        if (inTableCell != null)
                        {
                            if ((x > element2.RealLeft) && (x < (element2.RealLeft + element2.Width)))
                            {
                                if (element2 is ZYTextContainer)
                                {
                                    ZYTextTableCell cell2 = (ZYTextTableCell) element2;
                                    if (cell2 != null)
                                    {
                                        if (!cell2.VMContinue)
                                        {
                                            lastElement = inTableCell.GetLastElement();
                                        }
                                    }
                                    else
                                    {
                                        lastElement = ((ZYTextContainer) element2).GetLastElement();
                                    }
                                }
                                else
                                {
                                    lastElement = element2;
                                }
                                break;
                            }
                            if (inTableCell.Bounds.Contains(x, y))
                            {
                                foreach (ZYTextLine line4 in inTableCell.Lines)
                                {
                                    if ((line4.RealTop < y) && (y < (line4.RealTop + line4.Height)))
                                    {
                                        lastElement = (ZYTextElement) line4.Elements[line4.Elements.Count - 1];
                                        break;
                                    }
                                }
                            }
                            continue;
                        }
                        if ((x > element2.RealLeft) && (x < ((line3.RealLeft + element2.RealLeft) + element2.Width)))
                        {
                            if (element2 is ZYTextContainer)
                            {
                                lastElement = ((ZYTextContainer) element2).GetLastElement();
                            }
                            else
                            {
                                lastElement = element2;
                            }
                            break;
                        }
                    }
                    if (lastElement != null)
                    {
                        line2 = line3;
                        break;
                    }
                }
                if (line2 == null)
                {
                    line2 = (ZYTextLine) list[0];
                }
                if (lastElement == null)
                {
                    if (line2.HasLineEnd)
                    {
                        index = this.myElements.IndexOf(line2.LastElement);
                    }
                    else
                    {
                        index = this.myElements.IndexOf(line2.LastElement) + 1;
                        flag = true;
                    }
                }
                else
                {
                    index = this.myElements.IndexOf(lastElement);
                    flag = false;
                }
                if (index > this.myElements.Count)
                {
                    index = this.myElements.Count - 1;
                    flag = false;
                }
                if (index < 0)
                {
                    index = 0;
                    flag = false;
                }
                this.MoveSelectStart(index);
                if (this.bolLineEndFlag != flag)
                {
                    this.bolLineEndFlag = flag;
                    ((ZYTextDocument) this.myDocument).UpdateTextCaret();
                }
            }
        }

        public void MoveUpOneLine()
        {
            ZYTextLine preLine = this.PreLine;
            if (preLine != null)
            {
                if (this.intLastXPos <= 0)
                {
                    ZYTextElement element = (ZYTextElement) this.myElements[this.intSelectStart];
                    this.intLastXPos = element.RealLeft;
                }
                foreach (ZYTextElement element2 in preLine.Elements)
                {
                    if (element2.RealLeft >= this.intLastXPos)
                    {
                        this.MoveSelectStart(element2);
                        return;
                    }
                }
                this.MoveSelectStart(preLine.LastElement);
            }
        }

        public void MoveUpOneLine2()
        {
            ZYTextElement element = (ZYTextElement) this.myElements[this.intSelectStart];
            int num = element.RealLineIndex - 1;
            int realLeft = element.RealLeft;
            ZYTextElement element2 = null;
            for (int i = this.intSelectStart - 1; i >= 0; i--)
            {
                element2 = (ZYTextElement) this.myElements[i];
                if ((element2.RealLineIndex == num) && (element2.RealLeft <= realLeft))
                {
                    this.MoveSelectStart(i);
                    break;
                }
                if (element2.RealLineIndex < num)
                {
                    this.MoveSelectStart((int) (i + 1));
                    break;
                }
            }
        }

        public bool ReplaceSelection(string strText)
        {
            if ((strText == null) || (strText.Length == 0))
            {
                return false;
            }
            this.DeleteSeleciton();
            this.InsertString(strText.Replace("\r\n", "\r"));
            this.bolModified = true;
            return true;
        }

        public bool ReplaceText(string strFind, string strReplace)
        {
            if (this.GetSelectText() == strFind)
            {
                this.ReplaceSelection(strReplace);
            }
            this.FindText(strFind);
            return true;
        }

        public int ReplaceTextAll(string strFind, string strReplace)
        {
            int num = 0;
            this.SetSelection(0, 0);
            while (this.FindText(strFind))
            {
                this.ReplaceSelection(strReplace);
                num++;
            }
            return num;
        }

        public void SelectAll()
        {
            this.SetSelection(0, this.myElements.Count);
        }

        public bool SetSelection(int NewSelectStart, int NewSelectLength)
        {
            this.bolLineEndFlag = false;
            if ((this.myElements == null) || (this.myElements.Count == 0))
            {
                return false;
            }
            int index = 0;
            int num2 = 0;
            int num3 = 0;
            int num4 = 0;
            int num5 = 0;
            NewSelectStart = this.FixIndex(NewSelectStart);
            int num6 = (NewSelectStart > this.intSelectStart) ? 1 : -1;
            bool flag = NewSelectLength == 0;
            for (int i = NewSelectStart; (i >= 0) && (i < this.myElements.Count); i += num6)
            {
                ZYTextElement element = (ZYTextElement)this.myElements[i];
                if ((element is ZYTextContainer) || !element.Parent.WholeElement)
                {
                    NewSelectStart = i;
                    break;
                }
                if (!flag)
                {
                    NewSelectLength -= num6;
                }
            }
            if ((this.intSelectStart != NewSelectStart) || (this.intSelectLength != NewSelectLength))
            {
                int intSelectStart = this.intSelectStart;
                if ((NewSelectLength == 0) && (this.intSelectLength == 0))
                {
                    this.intSelectStart = NewSelectStart;
                    if (this.intSelectStart < 0)
                    {
                        this.intSelectStart = 0;
                    }
                    if (this.intSelectStart >= this.myElements.Count)
                    {
                        this.intSelectStart = this.myElements.Count - 1;
                    }
                    ZYTextElement element2 = (ZYTextElement)this.myElements[this.intSelectStart];
                    if (this.myDocument != null)
                    {
                        this.myDocument.SelectionChanged(intSelectStart, 0, this.intSelectStart, 0);
                    }
                    if ((intSelectStart >= 0) && (intSelectStart < this.myElements.Count))
                    {
                        ((ZYTextElement)this.myElements[intSelectStart]).HandleLeave();
                    }
                    ((ZYTextElement)this.myElements[this.intSelectStart]).HandleEnter();
                    return true;
                }
                if (this.intSelectLength > 0)
                {
                    index = this.intSelectStart;
                    num2 = this.intSelectStart + this.intSelectLength;
                }
                else
                {
                    index = this.intSelectStart + this.intSelectLength;
                    num2 = this.intSelectStart;
                }
                if (NewSelectLength > 0)
                {
                    num3 = NewSelectStart;
                    num4 = NewSelectStart + NewSelectLength;
                }
                else
                {
                    num3 = NewSelectStart + NewSelectLength;
                    num4 = NewSelectStart;
                }
                if (index > num3)
                {
                    num5 = index;
                    index = num3;
                    num3 = num5;
                }
                if (num2 > num4)
                {
                    num5 = num2;
                    num2 = num4;
                    num4 = num5;
                }
                if (num3 > num2)
                {
                    num5 = num3;
                    num3 = num2;
                    num2 = num5;
                }
                this.intSelectStart = NewSelectStart;
                this.intSelectLength = NewSelectLength;
                this.FixSelection();
                index = this.FixIndex(index);
                num2 = this.FixIndex(num2);
                num3 = this.FixIndex(num3);
                num4 = this.FixIndex(num4);
                if (index != num3)
                {
                    for (int j = index; j <= num3; j++)
                    {
                        if (((ZYTextElement)this.myElements[j]).HandleSelectedChange())
                        {
                            return false;
                        }
                    }
                }
                if (num2 != num4)
                {
                    for (int k = num2; k <= num4; k++)
                    {
                        if (((ZYTextElement)this.myElements[k]).HandleSelectedChange())
                        {
                            return false;
                        }
                    }
                }
                if (this.myDocument != null)
                {
                    this.myDocument.SelectionChanged(index, num2, num3, num4);
                }
                if ((intSelectStart >= 0) && (intSelectStart < this.myElements.Count))
                {
                    ((ZYTextElement)this.myElements[intSelectStart]).HandleLeave();
                }
                ((ZYTextElement)this.myElements[this.intSelectStart]).HandleEnter();
            }
            return true;
        }

        public int AbsSelectEnd
        {
            get
            {
                int num;
                if (this.intSelectLength >= 0)
                {
                    num = (this.intSelectStart + this.intSelectLength) - 1;
                }
                else
                {
                    num = this.intSelectStart - 1;
                }
                if (num >= (this.myElements.Count - 1))
                {
                    num = this.myElements.Count - 1;
                }
                return num;
            }
        }

        public int AbsSelectStart
        {
            get
            {
                return ((this.intSelectLength > 0) ? this.intSelectStart : (this.intSelectStart + this.intSelectLength));
            }
        }

        public bool AutoClearSelection
        {
            get
            {
                return this.bolAutoClearSelection;
            }
            set
            {
                this.bolAutoClearSelection = value;
            }
        }

        public ZYTextElement CurrentElement
        {
            get
            {
                ZYTextElement parent = null;
                if (this.myElements.Count == 0)
                {
                    return null;
                }
                if (((this.myElements != null) && (this.intSelectStart >= 0)) && (this.intSelectStart < this.myElements.Count))
                {
                    parent = (ZYTextElement) this.myElements[this.intSelectStart];
                }
                else
                {
                    parent = (ZYTextElement) this.myElements[this.myElements.Count - 1];
                }
                if (parent.Parent.WholeElement)
                {
                    parent = parent.Parent;
                }
                return parent;
            }
            set
            {
                if (this.myElements.Contains(value))
                {
                    this.MoveSelectStart(this.myElements.IndexOf(value));
                }
                this.intSelectStart = this.FixIndex(this.intSelectStart);
            }
        }

        /***
         * 获取当前所在行
         */
        public ZYTextLine CurrentLine
        {
            get
            {
                if (this.myElements.Count == 0)
                {
                    return null;
                }
                if (((this.myElements != null) && (this.intSelectStart >= 0)) && (this.intSelectStart < this.myElements.Count))
                {
                    ZYTextLine ownerLine = ((ZYTextElement) this.myElements[this.intSelectStart]).OwnerLine;
                    if (this.bolLineEndFlag && (this.myDocument.Lines.IndexOf(ownerLine) > 0))
                    {
                        return (ZYTextLine) this.myDocument.Lines[this.myDocument.Lines.IndexOf(ownerLine) - 1];
                    }
                    return ownerLine;
                }
                return ((ZYTextElement) this.myElements[this.myElements.Count - 1]).OwnerLine;
            }
        }

        public ZYTextElement CurrentSelectElement
        {
            get
            {
                if ((this.myElements.Count == 0) || ((this.intSelectLength != 1) && (this.intSelectLength != -1)))
                {
                    return null;
                }
                return (ZYTextElement) this.myElements[this.AbsSelectStart];
            }
            set
            {
                if (this.myElements.Contains(value))
                {
                    this.SetSelection(this.myElements.IndexOf(value) + 1, -1);
                }
            }
        }

        /***
         * 操作类
         */
        public IZYContentDocument Document
        {
            get
            {
                return this.myDocument;
            }
            set
            {
                this.myDocument = value;
            }
        }

        /***
         * 设置元素集合
         */
        public ArrayList Elements
        {
            get
            {
                return this.myElements;
            }
            set
            {
                this.myElements = value;
                this.bolModified = false;
                this.strFixLenText = null;
                this.SetSelection(0, 0); //设置当前选中元素
            }
        }

        public bool LineEndFlag
        {
            get
            {
                return this.bolLineEndFlag;
            }
        }

        public bool Modified
        {
            get
            {
                return this.bolModified;
            }
            set
            {
                this.bolModified = value;
            }
        }

        /***
         * 获取当前所在行的下一行
         */
        public ZYTextLine NextLine
        {
            get
            {
                ZYTextLine line2=null;
                try
                {
                    ZYTextLine currentLine = this.CurrentLine;
                    if (this.myDocument.Lines.IndexOf(currentLine) < (this.myDocument.Lines.Count - 1))
                    {
                        for (int i = this.intSelectStart + 1; i < this.myElements.Count; i++)
                        {
                            ZYTextElement element = (ZYTextElement) this.myElements[i];
                            if (element.OwnerLine != currentLine)
                            {
                                return element.OwnerLine;
                            }
                        }
                        return null;
                    }
                    line2 = currentLine;
                }
                catch
                {
                }
                return line2;
            }
        }

        /***
         * 获取当前选中元素的前一个元素
         * 
         */
        public ZYTextElement PreElement
        {
            get
            {
                if (((this.myElements != null) && (this.myElements.Count > 0)) && ((this.intSelectStart > 0) && (this.intSelectStart < this.myElements.Count)))
                {
                    return (ZYTextElement) this.myElements[this.intSelectStart - 1];
                }
                return null;
            }
        }

        /***
         * 获取当前所在行的上一行
         */
        public ZYTextLine PreLine
        {
            get
            {
                ZYTextLine line2=null;
                try
                {
                    ZYTextLine currentLine = this.CurrentLine;
                    if (this.myDocument.Lines.IndexOf(currentLine) > 0)
                    {
                        for (int i = this.intSelectStart - 1; i >= 0; i--)
                        {
                            ZYTextElement element = (ZYTextElement) this.myElements[i];
                            if (element.OwnerLine != currentLine)
                            {
                                return element.OwnerLine;
                            }
                        }
                        return null;
                    }
                    line2 = currentLine;
                }
                catch
                {
                }
                return line2;
            }
        }

        /***
         * 选中元素长度
         */
        public int SelectLength
        {
            get
            {
                return this.intSelectLength;
            }
        }

        /***
         * 获取或设置选中元素下标
         */
        public int SelectStart
        {
            get
            {
                return this.intSelectStart;
            }
            set
            {
                if (this.bolAutoClearSelection)
                {
                    this.SetSelection(value, 0);
                }
                else
                {
                    this.SetSelection(value, this.intSelectStart - value);
                }
            }
        }
    }
}

